package com.winning.sjzc.sjygl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winning.sjzc.sjygl.dao.ElementManageDao;
import com.winning.sjzc.sjygl.entity.SJYXX;
import com.winning.sjzc.sjygl.entity.ZdbEntity;
import com.winning.sjzc.sjygl.service.ElementManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ElementManageServiceImpl implements ElementManageService {

    @Autowired
    private ElementManageDao elementManageDao;

    @Override
    public PageInfo<SJYXX> queryElementList(int offset, int  limit, String  sjymc) {
       PageHelper.startPage((offset/limit)+1,limit);
        Map<String, Object> map =new HashMap<>();
        if(StringUtils.isNotBlank(sjymc) ){
            map.put("sjymc", sjymc);
        }
        List<SJYXX> queryElementListByPage = elementManageDao.queryElementListByPage(map);
        PageInfo<SJYXX> page = new PageInfo<>(queryElementListByPage);
        return page;
    }

    @Override
    public void deleteElementBySjydm(String sjydm) {
        Map<String, Object> map =new HashMap<>();
        map.put("sjydm", sjydm);
        elementManageDao.deleteElementBySjydm(map);
    }

    @Override
    public int saveElement(SJYXX sjyxx) {
        String sjydm = sjyxx.getSJYDM();
        SJYXX qSjyxx = querySjyxxBySjydm(sjydm);
        if(qSjyxx != null && qSjyxx.getSJYDM() !=""){
            return -1;
        }
        elementManageDao.saveElement(sjyxx);
        return 1;
    }

    @Override
    public SJYXX querySjyxxBySjydm(String sjydm) {
        SJYXX sjyxx=elementManageDao.querySjyxxBySjydm(sjydm);
        return sjyxx;
    }

    @Override
    public int updateElement(SJYXX sjyxx) {
        elementManageDao.updateElement(sjyxx);
        return 1;
    }

    //初始化标准来源和数据元类型
    @Override
    public  List <ZdbEntity>  initAddData(String zblbdm) {
        String[] arr=zblbdm.split(",");
        List <ZdbEntity> list=elementManageDao.initAddData(arr);
        return list;
    }
}
